//index.js
//获取应用实例
const app = getApp()
var util = require('../../utils/util.js')
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    checkpoint:0,
    is_autoplay:false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
    //广告跳转
    avdtap:function (e) {
        wx.navigateToMiniProgram({
            appId: e.currentTarget.dataset.appid,
            path: 'pages/index/index',
            success:function(res) {
                console.log(res)
            }
        })
    },
    //天鱼游戏盒子
    float:function () {
        wx.navigateToMiniProgram({
            appId: "wxaa3e45e4b6a1bbd0",
            path: 'pages/index/index',
            success:function(res) {
                console.log(res)
            }
        })
    },
  onLoad: function () {
      wx.getSystemInfo({
          success: function(res) {
              console.log(res.platform)
              app.globalData.platform = res.platform;
          }
      })
    if (app.globalData.userInfo) {
      this.login()
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.login()
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.login()
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    this.adver()
      
  },
    adver:function () {
      var that = this
        wx.request({
            url: 'https://caige1.sanliwenhua.com/all_advs',
            method: "get",
            success: function (res) {
                //console.log(res, "广告")
                if (res.data.code = 200) {
                    var AdvData = res.data.data;
                    that.setData({
                        avdUrl: AdvData//[0].pic,
                    })
                }
            }
        })
        wx.request({
            url: 'https://caige1.sanliwenhua.com/switch',
            method: "get",
            success: function (res) {
                //console.log(res, "开关")
                if (res.data.code = 200) {
                    var isADV = res.data.data.isADV;
                    that.setData({
                        isADV: isADV //广告开关写死
                    })
                } else {

                }
            }
        })
        
    },
    ShoppingMallTap: function () {
        wx.navigateToMiniProgram({
            appId: 'wxaa9104bb31aa9b96',
            path: 'pages/index/index',
            envVersion: 'release',
            success: function (res) {
                //console.log(res);
            },
            fail: function (res) {
                //console.error(res);
                wx.showToast({
                    title: res.data.message,
                    image: '/image/errIcon.png',
                    duration: 2000
                })
            }
        })
    },
    //登录接口
  login:function () {
    var that = this
      console.log(app.globalData)
      // wx.showLoading({
      //     title: '加载中...',
      // })
      if(app.globalData.userInfo){
          var dataSend = {
              code: app.globalData.code,
              encryptedData: app.globalData.encryptedData,
              iv: app.globalData.iv,
              icon: app.globalData.userInfo.avatarUrl,
              nickname: app.globalData.userInfo.nickName
          }
          console.log(app.globalData.code)
          wx.request({
              url: 'https://caige1.sanliwenhua.com/idiom/wx/onLogin',
              method: "get",
              data: dataSend,
              success: function (res) {
                  console.log(res)
                  if(res.data.code == 200){
                      var token = res.data.data.x_token;
                      var checkpoint = res.data.data.checkpoint;
                      var id =  res.data.data.id;
                      var session_key = res.data.data.session_key;
                      app.globalData.userInfo.token = token;
                      app.globalData.userInfo.checkpoint = checkpoint;
                      app.globalData.userInfo.id = id;
                      app.globalData.userInfo.session_key = session_key;
                      //that.update()
                      that.setData({
                          checkpoint: checkpoint
                      })
                      if(that.data.is_autoplay){
                          that.setData({
                              is_autoplay: false,
                          })
                          setTimeout(function(){
                              wx.hideLoading()
                              wx.navigateTo({
                                  url: '../Details/Details'
                              })
                          },1000)
                      }
                  }
              },
              fail:function (res) {
                  wx.showToast({
                      title: res.data.message,
                      icon: 'success',
                      duration: 2000
                  })
              }
          })
      }
  },
    //获取群id接口
  update:function () {
      var that = this
      var NewTime = new Date().getTime().toString();
      var key = util.sha1(app.globalData.userInfo.id + NewTime + app.globalData.userInfo.checkpoint + app.globalData.userInfo.id);
      key = key.toUpperCase()
      var data = {}
      console.log('update群场景shareTicket:',app.globalData)
      var update = function (data) {
          wx.request({
              url: 'https://caige1.sanliwenhua.com/idiom/update_checkpoint',
              method: "post",
              data: data,
              success: function (res) {
                  console.log(res)
                  app.globalData.group_id = res.data.group_id;
              },
              fail: function (res) {
                  console.log(res)
                  wx.showToast({
                      title: res.data.message,
                      icon: 'success',
                      duration: 2000
                  })
              }
          })
      }
      if(app.globalData.shareTicket){
          wx.getShareInfo({
              shareTicket:app.globalData.shareTicket,
              success:function (res) {
                  console.log(res)
                  app.globalData.Sharing= {}
                  app.globalData.Sharing.errMsg = res.errMsg
                  app.globalData.Sharing.encryptedData = res.encryptedData
                  app.globalData.Sharing.iv = res.iv
                  console.log(app.globalData.Sharing)
                  data = {
                      key: key,
                      time: NewTime,
                      checkpoint: app.globalData.userInfo.checkpoint,
                      x_token: app.globalData.userInfo.token,
                      errMsg: res.errMsg,
                      encryptedData: res.encryptedData,
                      iv: res.iv,
                      session_key: app.globalData.userInfo.session_key
                  }
                  update(data);
              },
              fail:function (res) {
                  wx.showToast({
                      title: res.data.message,
                      icon: 'success',
                      duration: 2000
                  })
              }
          })
      }else{
          data = {
              key: key,
              time: NewTime,
              checkpoint: app.globalData.userInfo.checkpoint,
              x_token: app.globalData.userInfo.token
          }
          update(data);
      }
  },
    //点击登录按钮
  getUserInfo: function(e) {
    console.log(e)
     this.setData({
         is_autoplay:true
     })
      wx.showLoading({
          title: '加载中...',
      })
      if(e.detail.userInfo){
          app.globalData.userInfo = e.detail.userInfo
          this.login()
          this.setData({
              userInfo: e.detail.userInfo,
              hasUserInfo: true
          })
      }
  },
    onShow:function () {
        wx.showShareMenu({
            withShareTicket: true
        })
        if(app.globalData.userInfo){
            //this.update()
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true,
                checkpoint: app.globalData.userInfo.checkpoint
            })
        }
    },
    onShareAppMessage: function (res) {
        var that = this;
        var nickeName = that.data.userInfo.nickName;
        if (res.from === 'button') {

        }
        return {
            title: '['+nickeName+'@我],那些年学过的成语，还记得吗？快来挑战吧！',
            path: 'pages/index/index',
            imageUrl: 'http://paz0eiqln.bkt.clouddn.com/image/fenxing.jpg',
            success: function (res) {
                wx.showToast({
                    title: '分享成功!',
                    icon: 'success',
                    duration: 2000
                })
                console.log('分享到群的标记', res)
                /*if(res.shareTickets){
                    app.globalData.shareTicket = res.shareTickets[0]
                    if(app.globalData.platform == "android"){
                        this.update()
                    }
                    console.log(app.globalData.Sharing)
                }else{
                  wx.showModal({
                        title: "提示",
                        content: "分享到群可以看到朋友的排行哦!",
                        showCancel: false,
                        confirmText: "确定"
                    })
                }*/
            },
            fail: function (res) {
                wx.showToast({
                    title: res.data.message,
                    icon: 'success',
                    duration: 2000
                })
                /*wx.showToast({
                    title: '分享失败！',
                    image: '/image/errIcon.png',
                    duration: 2000
                })*/
            }
        }
    }
})
